using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO.ProductionSystem;
using ThridGroup.ERP.DTO.ShiftSchedulingManagementDto;
using ThridGroup.ERP.ProductionSystem;

namespace ThridGroup.ERP.Web.Controllers.Workforcemanagement
{ 
    /// <summary>
    /// 排班管理控制器
    /// </summary>
    [Route("Workforcemanagement")]
    public class WorkforcemanagementController : Controller
    {
        private readonly ITeamServices _teamServices;
        private readonly EquipmentTypeIService _equipmentTypeService;
        private readonly IShiftScheduleService _shiftScheduleService;
     

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="teamServices">班组服务</param>
        /// <param name="equipmentTypeService">设备类型服务</param>
        /// <param name="shiftScheduleService">排班服务</param>
        public WorkforcemanagementController(ITeamServices teamServices, EquipmentTypeIService equipmentTypeService, IShiftScheduleService shiftScheduleService) 
        {
            _teamServices = teamServices;
            _equipmentTypeService = equipmentTypeService;
            _shiftScheduleService = shiftScheduleService;
        }

        [HttpGet("ShiftschedulecalendarView")]
        public IActionResult ShiftschedulecalendarView()
        {
            return View();
        }

        /// <summary>
        /// 显示设备类型信息（分页查询）
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost("GetEquipmentType")]
        [ValidateAntiForgeryToken]
        public async Task<JsonResult> GetEquipmentType(int pageIndex = 1, int pageSize = 10)
        {
            var list = await _equipmentTypeService.GetEquipmentType();
            var result = list.OrderBy(c => c.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return Json(new
            {
                code = 0,
                msg = "",
                count = list.Count,
                data = result,
            });
        }

        /// <summary>
        /// 显示班组信息（分页查询）
        /// </summary>
        /// <param name="dto">查询条件</param>
        /// <returns></returns>
        [HttpPost("QueryTeam")]
        [ValidateAntiForgeryToken]
        public async Task<JsonResult> QueryTeam(GetTeamDto dto)
        {
            var list = await _teamServices.QueryTeam(dto);

            var teamList = list.OrderByDescending(a => a.Id)
                .Skip((dto.page - 1) * dto.limit)
                .Take(dto.limit)
                .ToList();

            return Json(new
            {
                code = 0,
                msg = "",
                count = list.Count,
                data = teamList
            });
        }

        /// <summary>
        /// 获取排班日历数据
        /// </summary>
        [HttpPost("GetShiftData")]
        [ValidateAntiForgeryToken]
        public async Task<JsonResult> GetShiftData(int year, int month, string equipmentTypeCode, string teamNumber)
        {
            var data = await _shiftScheduleService.GetShiftSchedules(year, month, equipmentTypeCode, teamNumber);
            return Json(new { code = 0, data });
        }

        /// <summary>
        /// 保存排班数据
        /// </summary>
        [HttpPost("SaveShift")]
        [ValidateAntiForgeryToken]
        public async Task<JsonResult> SaveShift([FromBody] ThridGroup.ERP.DTO.ProductionSystem.ShiftScheduleDto dto)
        {
            await _shiftScheduleService.SaveShiftSchedule(dto);
            return Json(new { code = 0, msg = "保存成功" });
        }
    }
} 